package com.guagua.chat.room;
/**
 * 北京八度互联科技有限公司版权所有
 * Copyright (C) Badu Corporation. All Rights Reserved
 */
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.guagua.chat.photo.Photo;
import com.guagua.chat.photo.PhotoDao;
import com.guagua.vas.constants.CacheConstants;
import com.guagua.vas.redis.RedIsHandlerFactory;
import com.guagua.vas.util.CacheManager;
import com.guagua.vas.util.StringUtil;
/** ******************************************************************
* 类概要:房间管理类
 * @author 白云鹏
 * @version 2012年7月31日10:11:39
*******************************************************************/
public class ChatRoomManager {
	private final int tempRoomMastFlag = 0;
	private static ChatRoomManager mgr = null;
	private ChatRoomManager() {
	}
	/**
	 * 函数说明：获取单例房间管理对象 <br/>
	 * 
	 * @return 返回结果说明. （ChatRoomManager）房间管理对象
	 */
	public static ChatRoomManager getInstance() {
		
		if (null == mgr) {
			mgr = new ChatRoomManager();
		}
		return mgr;
	}
	
	private ChatRoomDao dao=new ChatRoomDao();
	private PhotoDao phDao=new PhotoDao();
	
	/**
	 * 获取是否是室主和 周一道周五的房间人气值
	 * @param room_id
	 * @param userid
	 * @return
	 * 	   	list.get(0)  ==  是否是室主,0:非室主，1 室主
	 * 		list.get(1)  ==  人气值，  数量值，小于20就不能发布到活动中心。 
	 */
	public List getOnlineWeekAndMaster(long room_id,long userid){
		List result = new ArrayList();
		//访问用户是否是室主的判断标记，0为非室主，1为室主
		int roomMastFlag=tempRoomMastFlag;
		String room_name="此房间名称已过期";
		Map<String,String> mapRoomMsg = null;
		//从公用redis中查询房间的基本信息，主要是获取房间名称和室主
		mapRoomMsg = RedIsHandlerFactory.getInstance().getGuaguaRoomMsgMap(room_id);
		String [] masters=null;
		int online_week=0;//周人气-周一至周五
		if(mapRoomMsg!=null && mapRoomMsg.size()>0){
			room_name=StringUtil.StringDecodeUtf8(mapRoomMsg.get(CacheConstants.ROOM_NAME));
			String roomMaster=mapRoomMsg.get(CacheConstants.ROOM_MASTER);
			masters=(roomMaster==null?null:roomMaster.split(","));
			String online_str=mapRoomMsg.get(CacheConstants.ROOM_ONLINE_WEEK);
			online_week=(online_str==null?0:Integer.parseInt(online_str));
		}
		//临时解决措施，从数据库中查询室主ID
		//masters=dao.getRoomMasterID(room_id).split(",");
		if (masters!=null){
			for (String str:masters){
				//判断用户是否为室主
				if (str.equals(String.valueOf(userid))){
					roomMastFlag=1;
					break;
				}
			}
		}
		result.add(roomMastFlag);
		result.add(online_week);
		result.add(room_name);
		return result;
	}
	
	/**
	 * 函数说明：根据房间id查询房间基本信息 <br/>
	 * 
	 * @param room_id
	 *            类型:(long)；<br/> 房间id<br/>；
	 * @return 返回结果说明. （List）房间信息集合
	 */
	public List getRoomInfo(long room_id,long userid){
		List room_info = new ArrayList();
		Map<String,String> mapRoomMsg = null;
		//访问用户是否是室主的判断标记，0为非室主，1为室主
		int roomMastFlag=tempRoomMastFlag;
		String [] masters=null;
		String room_name="此房间名称已过期";
		int online_week=0;//周人气-周一至周五
		//从缓存中查询
		String key = ChatRoomUtil.ROOMINFO_ROOM_ID(room_id);
		room_info= (List)CacheManager.getInstance().get(key);
		if (room_info==null){
			//缓存中没有数据，从数据库中查询
			room_info=dao.getRoomInfo(room_id);
			//将结果放入缓存中，缓存时间为5分钟\
			CacheManager.getInstance().put(key, room_info, 5,TimeUnit.MINUTES);
		}
		//从公用redis中查询房间的基本信息，主要是获取房间名称和室主
		mapRoomMsg = RedIsHandlerFactory.getInstance().getGuaguaRoomMsgMap(room_id);
		if(mapRoomMsg!=null && mapRoomMsg.size()>0){
			room_name=StringUtil.StringDecodeUtf8(mapRoomMsg.get(CacheConstants.ROOM_NAME));
			String roomMaster=mapRoomMsg.get(CacheConstants.ROOM_MASTER);
			masters=(roomMaster==null?null:roomMaster.split(","));
			String online_str=mapRoomMsg.get(CacheConstants.ROOM_ONLINE_WEEK);
			online_week=(online_str==null?0:Integer.parseInt(online_str));
		}
		//临时解决措施，从数据库中查询室主ID
		//masters=dao.getRoomMasterID(room_id).split(",");
		if (masters!=null){
			for (String str:masters){
				//判断用户是否为室主
				if (str.equals(String.valueOf(userid))){
					roomMastFlag=1;
					break;
				}
			}
		}
		
		if(room_info.size()>0){
			((HashMap)room_info.get(0)).put("name",room_name);
			((HashMap)room_info.get(0)).put("roomMaster",roomMastFlag);
			((HashMap)room_info.get(0)).put("onlineWeek",online_week);
		}else{
			Map map=new HashMap();
			map.put("room_id",room_id);
			map.put("name", room_name);
			map.put("roomMaster",roomMastFlag);
			map.put("onlineWeek", online_week);
			map.put("imgURL", "");
			room_info.add(map);
		}
		return room_info;
		
	}
	/**
	 * 函数说明：根据房间id编辑房间基本信息 <br/>
	 * 
	 * @param room
	 *            类型:(ChatRoomInfo)；<br/> 房间信息对象<br/>；
	 * @param photo
	 *            类型:(Photo)；<br/> 图片对象<br/>；
	 * @param userid
	 *            类型:(long)；<br/> 访问用户ID<br/>；
	 * @return 返回结果说明. （boolean）是否操作成功，true为成功，false为失败
	 */
	public boolean editRoomInfo(ChatRoomInfo room,Photo photo,long userid){
		boolean flag=false;
		//对图片地址进行处理
		if (photo.getPhoto_path().indexOf("?c=")>0){
			String imgPath=photo.getPhoto_path().replaceAll("http://", "").replaceAll("https://", "");
			imgPath=imgPath.substring(imgPath.indexOf("/"));
			photo.setPhoto_path(imgPath);
			photo.setCreator(userid);
			//添加头像
			long photoID=phDao.addPhoto(photo);
			if(photoID<=0){
				return false;
			}
			room.setRoom_face(photoID);
		}
		//判断房间信息是否已经存在
		if (room.getCreate_time()>0){
			//更新房间信息
			flag=dao.udateRoomInfo(room);
		}else{
			//新增房间信息
			flag=dao.addRoomInfo(room);
		}
		
		if(flag){//清除房间信息的缓存
			CacheManager.getInstance().remove(ChatRoomUtil.ROOMINFO_ROOM_ID(room.getRoom_id()));
		}
	
		return flag;
	}
	public boolean udateRoomType(long roomid,int sorttype){
		CacheManager.getInstance().remove(ChatRoomUtil.ROOMSINGER_ROOM_ID(roomid));
		CacheManager.getInstance().remove(ChatRoomUtil.ROOMSINGER_4_ROOM_ID(roomid));
		CacheManager.getInstance().remove(ChatRoomUtil.ROOMINFO_ROOM_ID(roomid));
//		ChatRoomUtil.removeLEVELICON_ROOM_ID(roomid);
		return dao.udateRoomType(roomid,sorttype);
	}
	/**
	 * 添加房间登录信息
	 * @param uid	用户ID
	 * @param roomid
	 * @param ip
	 * @param opTime
	 * @return
	 */
	public int addRoomLoginLog(long uid,long roomid,String ip){
		return dao.addRoomLoginLog(uid,roomid,ip);
	}
	
	/**
	 * 获取房主有哪些房间
	 * @param userId
	 * @return
	 */
	public List<RoomVo> getUserRoom(long userId){
		String key=ChatRoomUtil.USERROOM_USER_ID(userId);
		List<RoomVo> list = (List)CacheManager.getInstance().get(key);
		if(null == list || list.size()==0){//不存在，则
			list = dao.getUserRoom(userId);
			//10分钟清理缓存。然后当添加房间的时候，应该删除该缓存
			CacheManager.getInstance().put(key, list, 10, TimeUnit.MINUTES);
		}
		return list;
	}
	
}
